package org.ttitfly.workflow.engine.basic;

import java.io.File;
import java.io.FileFilter;

import org.ttitfly.workflow.engine.AbstractProcessFactory;
import org.ttitfly.workflow.engine.basic.parser.BasicXMLProcessParser;
import org.ttitfly.workflow.engine.basic.parser.XMLProcessParser;
import org.ttitfly.workflow.factory.ObjectFactory;


/**
 * 流程工厂基本功能实现
 * 
 * @author 潘海春
 * @version 2009-9-22
 */
public class XMLProcessFactory extends AbstractProcessFactory {

	/**
	 * 构造函数
	 * 
	 * @param objectFactory
	 * @param dir
	 */
	public XMLProcessFactory(ObjectFactory objectFactory, File dir) {
		this(new BasicXMLProcessParser(objectFactory), dir);
	}

	/**
	 * 构造函数
	 * 
	 * @param objectFactory
	 * @param dir
	 */
	public XMLProcessFactory(XMLProcessParser parser, File dir) {
		if (parser == null) {
			throw new IllegalArgumentException("参数[parser]不能为空！");
		}
		if (dir != null && dir.exists() && dir.isDirectory()) {
			for (File file : dir.listFiles(new FileFilter() {
				@Override
				public boolean accept(File pathname) {
					// 只选择xml文件
					return pathname.getName().toLowerCase().endsWith(".xml");
				}
			})) {
				put(parser.parse(file));
			}
		}
	}

}
